package org.jacp.demo.util;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.UIManager;

import com.explodingpixels.border.FocusStateMatteBorder;
import com.explodingpixels.macwidgets.BottomBarSize;
import com.explodingpixels.macwidgets.MacColorUtils;
import com.explodingpixels.painter.Painter;
import com.explodingpixels.widgets.WindowUtils;
import com.jgoodies.forms.factories.Borders;

public class ToolBar extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 7260522620386359592L;
    private Painter<Component> fBackgroundPainter;

    public ToolBar(final BottomBarSize size) {
	super();
	init(size);

    }

    public ToolBar(final int hint,final BottomBarSize size) {
	super();
	setLayout(new FlowLayout(hint, 4, 0));
	init(size);

    }

    private void init(final BottomBarSize size) {
    	final int height = size.getHeight() + 4;
    	setPreferredSize(new Dimension(-1, height));
	setOpaque(false);
	// if (MacUtils.shouldManuallyPaintTexturedWindowBackground()) {
	setBackgroundPainter(createTexturedWindowWorkaroundPainter());
	// }
	setBorder(Borders.createEmptyBorder("3dlu, 4dlu, 3dlu, 4dlu"));
	final FocusStateMatteBorder border = new FocusStateMatteBorder(0, 0, 1,
		0, MacColorUtils.getTexturedWindowToolbarBorderFocusedColor(),
		MacColorUtils.getTexturedWindowToolbarBorderUnfocusedColor(),
		this);

	setBorder(BorderFactory.createCompoundBorder(border, getBorder()));
	WindowUtils.installJComponentRepainterOnWindowFocusChanged(this);
    }

    /**
     * Creates a {@link Painter} that paints a Mac unified tool bar gradient.
     * 
     * @return a {@code Painter} that paints a Mac unified tool bar gradient.
     */
    public Painter<Component> createTexturedWindowWorkaroundPainter() {
	return new Painter<Component>() {

	    private final Color ACTIVE_TOP_GRADIENT_COLOR = UIManager.getColor("Button.highlight");//new Color(0xc8c8c8);
	    private final Color ACTIVE_BOTTOM_GRADIENT_COLOR = UIManager.getColor("Button.light");//new Color(0x9a9a9a);
	    private final Color INACTIVE_TOP_GRADIENT_COLOR = UIManager.getColor("Button.light");//new Color(0xe4e4e4);
	    private final Color INACTIVE_BOTTOM_GRADIENT_COLOR = UIManager.getColor("Button.light");//new Color(0xd1d1d1);

	    @Override
	    public void paint(final Graphics2D graphics2D,
		    final Component component, final int width, final int height) {
		final boolean containedInActiveWindow = WindowUtils
			.isParentWindowFocused(component);

		final Color topColor = containedInActiveWindow ? ACTIVE_TOP_GRADIENT_COLOR
			: INACTIVE_TOP_GRADIENT_COLOR;
		final Color bottomColor = containedInActiveWindow ? ACTIVE_BOTTOM_GRADIENT_COLOR
			: INACTIVE_BOTTOM_GRADIENT_COLOR;

		final GradientPaint paint = new GradientPaint(0, 1, topColor,
			0, height, bottomColor);
		graphics2D.setPaint(paint);
		graphics2D.fillRect(0, 0, width, height);
	    }
	};
    }

    public void setBackgroundPainter(final Painter<Component> painter) {
	fBackgroundPainter = painter;
    }

    @Override
    protected void paintComponent(final Graphics g) {
	if (fBackgroundPainter != null) {
	    final Graphics2D graphics2D = (Graphics2D) g.create();
	    fBackgroundPainter.paint(graphics2D, this, getWidth(), getHeight());
	    graphics2D.dispose();
	}

	// TODO see if we can get rid of this call to super.paintComponent.
	super.paintComponent(g);
    }

}
